﻿
/*
MvcForum jQuery Plugin

http://mvcforum.net

http://nasa8x.com

Copyright (c) 2011 Nasa8x

*/
(function ($) {


    $.extend({

        MvcForum: {

            Quote: {

                name: 'mvcforum_quote',

                add: function (val) {
                    var _a = $.Cookie.get(this.name);
                    $.Cookie.add(this.name, '|' + val + _a, { days: 0.5 });

                },

                remove: function (val) {
                    var _vals = $.Cookie.get(this.name).split('|');
                    var _a = $.grep(_vals, function (value) {
                        return value != val;
                    }).join('|');

                    $.Cookie.add(this.name, _a, { days: 0.5 });
                },
                clear: function () {
                    $.Cookie.remove(this.name);
                },
                init: function () {

                    $('.multi-quote').unbind('.mvcforum').bind('click.mvcforum', function (e) {

                        e.preventDefault();
                        var _this = $(this);
                        var _t = _this.attr('rel');
                        if (_this.hasClass('actived')) {
                            $.MvcForum.Quote.remove(_t);
                            _this.removeClass('actived');
                        }
                        else {
                            $.MvcForum.Quote.add(_t);
                            _this.addClass('actived');

                        }

                    });

                    var _vals = $.Cookie.get(this.name).split('|').join(",#quote_");
                    $(_vals).addClass("actived");

                }


            },

            Collapse: {

                name: 'mvcforum_collapse',
                open: function (val) {
                    var _vals = $.Cookie.get(this.name).split('|');
                    var _a = $.grep(_vals, function (value) {
                        return value != val;
                    }).join('|');

                    $.Cookie.add(this.name, _a, { days: 1 });
                    $('#panel_' + val).slideDown();

                },
                close: function (val) {

                    var _a = $.Cookie.get(this.name);
                    $.Cookie.add(this.name, '|' + val + _a, { days: 1 });
                    $('#panel_' + val).slideUp();

                },
                init: function () {

                    $('.collapse').each(function () {
                        var _t = $(this);
                        _t.unbind('.mvcforum').bind('click.mvcforum', function (e) {
                            e.preventDefault();
                            if (_t.hasClass('close')) {

                                _t.removeClass('close').addClass('open');
                                $.MvcForum.Collapse.close(_t.attr('id'));
                            }
                            else {
                                _t.removeClass('collapse-open').addClass('close');
                                $.MvcForum.Collapse.open(_t.attr('id'));
                            }

                        });
                        if (_t.hasClass('open')) {
                            $('#panel_' + _t.attr('id')).hide();
                        }
                    });


                    var _vals = $.Cookie.get(this.name).split('|');
                    $(_vals.join(',#')).removeClass('close').addClass("collapse-open");
                    $(_vals.join(',#panel_')).hide();
                }

            },

            Notification: {

                pageIndex: 0,
                url: '/Cpanel/Notification',
                init: function (url) {
                    this.url = url;
                    setInterval(function () {
                        $.getJSON(url, { cmd: 'check' }, function (data) {
                            if (data.Total) {
                                $('#_notifyCount').text(data.Total);
                            }

                        });

                    }, 60000);

                    $('#notify-prev').hide().bind('click', $.proxy(this.prev, this));
                    $('#notify-next').bind('click', $.proxy(this.next, this));
                },

                show: function () {
                    this.pageIndex = 1;
                    this.load();

                },
                load: function () {
                    var _p = this.pageIndex;

                    if (_p > 1) {
                        $('#notify-prev').show();
                    }
                    else {
                        $('#notify-prev').hide();
                    };

                    $.post(this.url, { cmd: 'list', p: _p }, function (data) {

                        if (data.length > 0) {
                            $('#notify-panel').html(data);
                        }
                    });

                    /* $('#notify-panel').load(this.url, { cmd: 'list', p: _p }, function () {


                    });*/
                },

                next: function () {

                    this.pageIndex++;
                    this.load();
                },
                prev: function () {

                    this.pageIndex--;
                    this.load();
                }


            },

            queryString: function (key) {
                key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regex = new RegExp("[\\?&]" + key + "=([^&#]*)", 'ig');
                var qs = regex.exec(window.location.href);
                if (qs == null)
                    return null;
                else
                    return qs[1];

            },

            scrollTo: function (target, duration) {
                if ($(target))
                    $('html,body').animate({ scrollTop: $(target).offset().top }, duration ? duration : 500);
            },


            toggleCheckbox: function (_state, _forClass) {

                // default
                var _class = 'input.selected';

                if (_forClass) _class = _forClass;

                $(_class).each(function () {
                    $(this).attr("checked", _state);
                });

            },
            getSelectedItems: function (_forClass) {
                var _items = new Array();
                var _class = 'input.selected:checked';
                if (_forClass) _class = _forClass;
                $(_class).each(function () {

                    _items.push($(this).val());

                });
                return _items;

            },

            jump: function (_target) {
                if (_target.length > 0) {

                    window.location = _target;

                }

            },

            alert: function (msg) {
                if ($.KoolBox) {
                    $.KoolBox.alert(msg);
                }
                else {
                    alert(msg);
                }

            },

            prompt: function (m, v) {
                if ($.KoolBox) {
                    $.KoolBox.prompt(m, { value: v });
                }
                else {
                    prompt(m, s);
                }
            },

            externalLinks: function () {

                $('#main a').filter(function () {
                    return this.hostname && this.hostname !== location.hostname;
                }).addClass('external-link').attr('target', '_blank');


            },


            remove: function (id) {
                $(id).fadeOut(500, function () {
                    $(this).remove();
                });
            },


            niceGrid: function () {
                // Set Gridview Style

                $('.gridview tr')
                        .hover(
                            function () {
                                $(this).addClass('row-hover');
                            },
                            function () {
                                $(this).removeClass('row-hover');
                            })


                        .click(function (e) {

                            if (!e.ctrlKey) {
                                $('.gridview tr.row-selected input.selected').attr('checked', false);
                                $('.gridview tr.row-selected').removeClass('row-selected');

                            };

                            $('.gridview tr.row-current').removeClass('row-current');
                            $(this).addClass('row-selected row-current');
                            $(this).find('input.selected').attr('checked', true);

                            // $(this).noSelect();

                        });
            },


            ajaxSubmit: function (f) {

                if (f) {
                 
                    $.Ajax.submit(f);
                }
                    
                else {

                    $('form.ajax').unbind('.mvcforum').bind("submit.mvcforum", function (e) {
                        e.preventDefault();
                        $.Ajax.submit($(this));
                        return false;
                    });
                }

            },


            ajaxLoad: function (url) {

                if (url)
                    $.Ajax.load(url);
                else {
                    $('a.ajax').unbind('.kaio').bind("click.mvcforum", function (e) {
                        e.preventDefault();
                        var a = $(this);
                        var o = $.extend({}, $.metadata ? a.metadata() : $.meta ? a.data() : {});
                        $.Ajax.load(a.attr('href'), o);
                        return false;
                    });
                }

            },


            onLoad: function () {
            
                this.Quote.init();

                this.Collapse.init();


                $('a.lock,a.un-lock,a.like,a.next-thread,a.prev-thread').unbind(".mvcforum").bind("click.mvcforum", function (e) {
                    e.preventDefault();
                    var _this = $(this);
                    _this.blur();
                    var _meta = _this.metadata();
                    if (!_meta.target) {
                        _meta.target = '#';
                    }

                    $.extend(_meta, {
                        callback: function () {
                            $.MvcForum.onLoad();
                        }
                    });

                    $.Ajax.load(_this.attr("href"), _meta);

                });

                $('a.confirm').unbind(".mvcforum").bind("click.mvcforum", function (e) {
                    e.preventDefault();

                    var _this = $(this);

                    $.KoolBox.confirm(_this.attr("title"), { title: 'MessageBox' }, function (r) {

                        if (r) {

                            var _o = _this.metadata();
                            $.extend(_o, {
                                target: '#',
                                callback: function () {
                                    $.MvcForum.onLoad();
                                }
                            });

                            $.Ajax.load(_this.attr("href"), _o);
                        }

                    });

                });


                $("a.report").unbind(".mvcforum").bind('click.mvcforum', function (e) {

                    e.preventDefault();

                    var _this = $(this);
                    _this.blur();
                    /*var _option = _this.metadata();*/

                    $.KoolBox.show(_this.attr("href"), { height: 230, type: 'ajax' });


                });


                //

                this.externalLinks();

                if ($.fn.iembed) {
                    // Auto Embed
                    $('.post-message a[href]').iembed();
                }

                if ($.fn.dropdown) {
                    $('li.dropdown').dropdown({ when: 'click' });     
                }
                

                if (typeof SyntaxHighlighter != "undefined") {
                    // code format
                    SyntaxHighlighter.all();
                }


            }

        }
    });

})(jQuery);